# -*- coding:utf-8 -*-
# file: gui.py
# Written by koya
import sys

import pygame

# 游戏里面的玩家界面, 包括Hp,Exp,Mp,Score等
class user_gui:

    def __init__( self, player_mode, max_hp, max_exp, max_mp, screen_width = 512, screen_height = 640):
        
        #player 1 or 2
        #player_mode = 'Player1' or player_mode = 'Player2'
        self.player_mode = player_mode

        self.hp_bar = pygame.image.load('data/gui/hp.png')
        self.exp_bar = pygame.image.load('data/gui/Exp.png')
        self.mp_bar = pygame.image.load('data/gui/mp.png')
        self.mp_image = pygame.image.load('data/gui/mp_tag.png')
        
        self._hp_bar = self.hp_bar
        self._exp_bar = self.exp_bar
        self._mp_bar = self.mp_bar

        self.border_bar = pygame.image.load('data/gui/Metal Bar.png')

        self.max_hp = max_hp
        self.max_exp = max_exp
        self.cur_hp = self.max_hp
        self.cur_exp = self.max_exp
        
        #---------------
        self.max_mp = max_mp
        self.cur_mp = 0
        self.mp_num = 0
        #----------------
        self.score = 0
        self.cur_level = "[?]"

        self.bomb_num = 0
        self.lives = 0

        self.screen_width = screen_width
        self.screen_height = screen_height

        self.font = pygame.font.Font('data/Hemi Head 426.TTF', 15)

    def set_player_info( self, player_hp, player_max_hp, player_exp, player_max_exp,\
               player_mp, player_max_mp, player_mp_num, player_score, player_lives = 0, player_bomb_num = 0 ):

        self.cur_hp = player_hp
        self.cur_exp = player_exp

        self.max_hp = player_max_hp
        self.max_exp = player_max_exp

        #--------------------------
        self.cur_mp = player_mp
        self.max_mp = player_max_mp
        self.mp_num = player_mp_num

        self.score = player_score

        self.lives = player_lives
        self.bomb_num = player_bomb_num

    def set_cur_level( self, cur_level ):

        self.cur_level = cur_level


    def update( self ):

       
        hp_scale = float(self.cur_hp) / float(self.max_hp) * self._hp_bar.get_width()
        exp_scale = float(self.cur_exp) / float(self.max_exp) * self._exp_bar.get_width()

        #---------------------------
        mp_scale = float(self.cur_mp) / float(self.max_mp) * self._mp_bar.get_width()
        self.mp_bar = pygame.transform.scale( self._mp_bar, ( mp_scale, self._mp_bar.get_height()))
        
        self.hp_bar = pygame.transform.scale( self._hp_bar, ( hp_scale, self._hp_bar.get_height()))
        self.exp_bar = pygame.transform.scale( self._exp_bar,( exp_scale, self._exp_bar.get_height()))

    def render( self, screen ):

        font_image = self.font.render( "Stage: %s" %self.cur_level, 1, (255,255,255) )
        screen.blit( font_image, (200,600) )
        
        if self.player_mode == 'Player1':

            #player 1
            font_image = self.font.render( "Hp  ", 1, (255,255,255) )
            screen.blit( font_image, (2,5) )

            screen.blit( self.hp_bar, (40,12) )
            screen.blit( self.border_bar, ( 37, 9 ) )
           
            str = "%s"%self.cur_hp + "/" + "%s"%self.max_hp
            font_image = self.font.render( "%s" %str, 1, (255,255,255) )
            screen.blit( font_image, (50 + self._hp_bar.get_width(),9) )


            font_image = self.font.render( "Exp ", 1, (255,255,255) )
            screen.blit( font_image, (2,20) )

            screen.blit( self.exp_bar, ( 40, 27) )
            screen.blit( self.border_bar, ( 37, 24 ) )

            str = "%s"%self.cur_exp + "/" + "%s"%self.max_exp
            font_image = self.font.render( "%s" %str, 1, (255,255,255) )
            screen.blit( font_image, (50 + self._exp_bar.get_width(),24) )

            #-----------------------------------
            font_image = self.font.render( "Mp ", 1, (255,255,255) )
            screen.blit( font_image, (2,35) )

            screen.blit( self.mp_bar, ( 40, 40) )
            screen.blit( self.border_bar, ( 37, 39 ) )

            str = "%s"%self.cur_mp + "/" + "%s"%self.max_mp
            font_image = self.font.render( "%s" %str, 1, (255,255,255) )
            screen.blit( font_image, (50 + self._mp_bar.get_width(),39) )

            font_image = self.font.render( "Magic Attack: ", 1, (255,255,255) )
            screen.blit( font_image, (2,55) )
            
            for i in xrange( int(self.mp_num) ):
                screen.blit( self.mp_image, (100 + 28 * i,53) )


            #------------------------------------------


            font_image = self.font.render( "Player1 Score:  %d" %self.score, 1, (255,255,255) )
            screen.blit( font_image, (2,600) )

            font_image = self.font.render( "Life:  %d" %self.lives, 1, (255,255,255) )
            screen.blit( font_image, (2,615) )
            font_image = self.font.render( "Bomb:  %d" %self.bomb_num, 1, (255,255,255) )
            screen.blit( font_image, (100,615) )

        if self.player_mode == 'Player2':

            #player 2
            font_image = self.font.render( "Hp  ", 1, (255,255,255) )
            screen.blit( font_image, (272,5) )

            screen.blit( self.hp_bar, (310,12) )
            screen.blit( self.border_bar, ( 307, 9 ) )

            str = "%s"%self.cur_hp + "/" + "%s"%self.max_hp
            font_image = self.font.render( "%s" %str, 1, (255,255,255) )
            screen.blit( font_image, (320 + self._hp_bar.get_width(),9) )

            font_image = self.font.render( "Exp ", 1, (255,255,255) )
            screen.blit( font_image, (272,20) )

            screen.blit( self.exp_bar, ( 310, 27) )
            screen.blit( self.border_bar, ( 307, 24 ) )

            str = "%s"%self.cur_exp + "/" + "%s"%self.max_exp
            font_image = self.font.render( "%s" %str, 1, (255,255,255) )
            screen.blit( font_image, (320 + self._exp_bar.get_width(),24) )

            #-----------------------------------
            font_image = self.font.render( "Mp ", 1, (255,255,255) )
            screen.blit( font_image, (272,35) )

            screen.blit( self.mp_bar, ( 310, 40) )
            screen.blit( self.border_bar, ( 307, 39 ) )

            str = "%s"%self.cur_mp + "/" + "%s"%self.max_mp
            font_image = self.font.render( "%s" %str, 1, (255,255,255) )
            screen.blit( font_image, (320 + self._mp_bar.get_width(),39) )

            font_image = self.font.render( "Magic Attack: ", 1, (255,255,255) )
            screen.blit( font_image, (272,55) )
            
            
            for i in xrange( int(self.mp_num) ):
                screen.blit( self.mp_image, (372 + 28 * i,53) )
            #---------------------------------------------


            font_image = self.font.render( "Player2 Score:  %d" %self.score, 1, (255,255,255) )
            screen.blit( font_image, (300,600) )
            font_image = self.font.render( "Life:  %d" %self.lives, 1, (255,255,255) )
            screen.blit( font_image, (300,615) )
            font_image = self.font.render( "Bomb:  %d" %self.bomb_num, 1, (255,255,255) )
            screen.blit( font_image, (400,615) )



   



